//
//  UserSetViewController.m
//  FlowerRainGround
//
//  Created by lanou on 16/4/29.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "UserSetViewController.h"
#import "MyCellItemModel.h"
#import "MyCustomTableViewCell.h"
#import "MyCellItemSectionModel.h"
#import "CreatCellModel.h"
#import "ScoreViewController.h"
#import "LoginViewController.h"
#import "PersonInformationViewController.h"
#import "CountManagerViewController.h"
@interface UserSetViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation UserSetViewController



- (void)creatCell{
    MyCellItemModel *item1 = [CreatCellModel initWithLeftImage:nil itemName:@"个人资料" itemCellType:(MyCellItemTypeSystem) itemRightImage:nil itemRightName:nil block:^{
        PersonInformationViewController *personVC = [[PersonInformationViewController alloc] init];
        [self.navigationController pushViewController:personVC animated:YES];;
    } ];
    
    MyCellItemModel *item2 = [CreatCellModel initWithLeftImage:nil itemName:@"账号管理" itemCellType:MyCellItemTypeSystem itemRightImage:nil itemRightName:nil block:^{
        CountManagerViewController *countVC = [[CountManagerViewController alloc] init];
        [self.navigationController pushViewController:countVC animated:YES];
    }];
    MyCellItemModel *item3 = [CreatCellModel initWithLeftImage:nil itemName:@"积分规则" itemCellType:MyCellItemTypeSystem itemRightImage:nil itemRightName:nil block:^{
        ScoreViewController *scoreVC = [[ScoreViewController alloc] init];
        [self.navigationController pushViewController:scoreVC animated:YES];

    }];
    
    CGFloat size = [SDImageCache sharedImageCache].getSize / 1000.0 /1000;
    NSString *string = [NSString stringWithFormat:@"缓存大小为：%.2fMB", size];

//    __weak UserSetViewController *user = self;
    MyCellItemModel *item4 = [CreatCellModel initWithLeftImage:nil itemName:@"清理缓存" itemCellType:MyCellItemTypeSystem itemRightImage:nil itemRightName:string  block:^{
        
        
        if (size > 0.0) {
           
            UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"确认清理" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            
            [controller addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
               
                [[SDImageCache sharedImageCache] clearDisk];
 
//                [self addObserver:self forKeyPath:item4.itemRightname options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"item4"];
                
                [controller dismissViewControllerAnimated:YES completion:^{
                     NSLog(@"111");
                    
                }];
                
            } ]];
            
           
            
            [self.navigationController presentViewController:controller animated:YES completion:nil];

        }
        
        

    }];
    
    MyCellItemModel *item5 = [CreatCellModel initWithLeftImage:nil itemName:@"启动推送" itemCellType:MyCellItemTypeSwitch itemRightImage:@"icon-new" itemRightName:nil  block:^{
        
    }];
    
#pragma mark-----分区---------
    MyCellItemSectionModel *section1 = [CreatCellModel initWithArray:@[item1, item2, item3, item4] sectionName:nil sectionHeaderHeight:0];
    MyCellItemSectionModel *section2 = [CreatCellModel initWithArray:@[item5] sectionName:nil sectionHeaderHeight:0];
    

    self.dataArray = @[section1,section2];
}

//- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context{
//    if ([keyPath isEqualToString:@"0.00"]) {
//        NSLog(@"00000");
//    }
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//去掉系统分割线
    [self.view addSubview:_tableView];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, kHeight - 400, kWidth - 20, 50);
    button.backgroundColor = [UIColor orangeColor];
    button.layer.cornerRadius = 5;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.block = ^(){
        [self goOut];
    };
    
    [button setTitle:@"退出应用" forState:UIControlStateNormal];
    [self.tableView addSubview:button];
    
    
    
    
    
    [self creatCell];
    self.navigationItem.title = @"设置";
    
    
    
    
//    self.view.backgroundColor = [UIColor whiteColor];
//    // Do any additional setup after loading the view.
}

- (void)goOut
{
    if (![[userManager getUsrID] isEqualToString:@" "] ) {
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"田园生活" message:@"退出当前应用" preferredStyle:UIAlertControllerStyleAlert];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alertVC dismissViewControllerAnimated:YES completion:nil];
        }]];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            //当有用户的信息时删除用户ID， 退到登陆界面
            [userManager cancelUserID];
            //            [userManager cancelUserName];
            //            [userManager cancelUserHeadImage];
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self presentViewController:loginVC animated:YES completion:nil];
        }]];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
 
}



- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    MyCellItemSectionModel *sh = self.dataArray[section];
    return sh.sectionName;
    
}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    MyCellItemSectionModel *sh = self.dataArray[section];
    
    return sh.itemArray.count;
}


//- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 50;
//}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *reuse = @"reuse";
    MyCellItemSectionModel *sectionModel = self.dataArray[indexPath.section];
    MyCellItemModel *itemmodel = sectionModel.itemArray[indexPath.row];
    
    MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[MyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    
    cell.item = itemmodel;
    
    return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MyCellItemSectionModel *sec = self.dataArray[indexPath.section];
    MyCellItemModel *item = sec.itemArray[indexPath.row];
    if (item.clickItemOperation) {
        item.clickItemOperation();
    }
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
